import { useReducer } from 'react';

export interface IUserManageReducers {
  searchValueTree: string;
  selectTreeKeys: string[];
  expandedTreeKeys: string[];
  autoExpandParent: boolean;
  rightTablePagination: Record<string, any>;
  leftTreeType: Record<string, any>;
  searchFormValues: Record<string, any>;
}

interface IActionType {
  type:
    | 'user-searchValueTree'
    | 'user-selectTreeKeys'
    | 'user-expandedTreeKeys'
    | 'user-autoExpandParent'
    | 'user-rightTablePagination'
    | 'user-leftTreeType'
    | 'user-searchFormValues'
    | 'remove-all-state';
  payload?: any;
}

export const useUserManageRendecer = () => {
  const initState: IUserManageReducers = {
    searchValueTree: '',
    selectTreeKeys: [],
    expandedTreeKeys: [],
    autoExpandParent: false,
    rightTablePagination: {
      order: 'desc',
      page: 1,
      rows: 10,
      sort: 'createTime',
    },
    leftTreeType: {},
    searchFormValues: {},
  };

  const reducer = (state: IUserManageReducers, actions: IActionType) => {
    switch (actions?.type) {
      case 'user-searchValueTree': //左侧树搜索值
        return {
          ...state,
          searchValueTree: actions?.payload,
        };
      case 'user-selectTreeKeys': //左侧树选中值
        return {
          ...state,
          selectTreeKeys: actions?.payload,
        };
      case 'user-expandedTreeKeys': //左侧树展开key
        return {
          ...state,
          expandedTreeKeys: actions?.payload,
        };
      case 'user-autoExpandParent': //左侧树是否可以点击展开
        return {
          ...state,
          autoExpandParent: actions?.payload,
        };
      case 'user-rightTablePagination': //保存分页参数
        return {
          ...state,
          rightTablePagination: actions?.payload,
        };
      case 'user-leftTreeType': //左侧点击后的参数
        return {
          ...state,
          leftTreeType: actions?.payload,
        };
      case 'user-searchFormValues': //右侧表单搜索
        return {
          ...state,
          searchFormValues: actions?.payload,
        };
      case 'remove-all-state': {
        return {
          ...state,
          ...initState,
        };
      }
      default:
        return {
          ...state,
        };
    }
  };

  const [state, userManDispatch] = useReducer(reducer, initState);
  return {
    ...state,
    userManDispatch,
  };
};

export interface IUserOtherHandleReducers {
  currentStep: number;
  secopeLeftSearValue: string;
  twoLeftExpandedKeys: string[];
  twoAutoExpandParent: boolean;
  twoLeftSelectKeys: string[];
  twoLeftSelectRows: Record<string, any>[];
  secopeRightSearValue: string;
  userDetailSearchValue: string;
}

interface IOtherHandleType {
  type:
    | 'otherHandle-currentStep'
    | 'otherHandle-secopeLeftSearValue'
    | 'otherHandle-twoLeftExpandedKeys'
    | 'otherHandle-twoAutoExpandParent'
    | 'otherHandle-twoLeftSelectKeys'
    | 'otherHandle-twoLeftSelectRows'
    | 'otherHandle-secopeRightSearValue'
    | 'otherHandle-userDetailSearchValue'
    | 'remove-allOtherHandle-state';
  payload?: any;
}

export const useUserOtherHandleRendecer = () => {
  const otherInitState: IUserOtherHandleReducers = {
    currentStep: 0,
    secopeLeftSearValue: '',
    twoLeftExpandedKeys: [],
    twoAutoExpandParent: false,
    twoLeftSelectKeys: [],
    twoLeftSelectRows: [],
    secopeRightSearValue: '',
    userDetailSearchValue: '',
  };

  const otherReducer = (
    state: IUserOtherHandleReducers,
    actions: IOtherHandleType,
  ) => {
    switch (actions?.type) {
      case 'otherHandle-userDetailSearchValue':
        return {
          ...state,
          userDetailSearchValue: actions?.payload,
        };

      case 'otherHandle-secopeRightSearValue':
        return {
          ...state,
          secopeRightSearValue: actions?.payload,
        };

      case 'otherHandle-twoLeftSelectRows':
        return {
          ...state,
          twoLeftSelectRows: actions?.payload,
        };

      case 'otherHandle-twoLeftSelectKeys':
        return {
          ...state,
          twoLeftSelectKeys: actions?.payload,
        };

      case 'otherHandle-twoAutoExpandParent':
        return {
          ...state,
          twoAutoExpandParent: actions?.payload,
        };

      case 'otherHandle-twoLeftExpandedKeys':
        return {
          ...state,
          twoLeftExpandedKeys: actions?.payload,
        };

      case 'otherHandle-secopeLeftSearValue':
        return {
          ...state,
          secopeLeftSearValue: actions?.payload,
        };

      case 'otherHandle-currentStep':
        return {
          ...state,
          currentStep: actions?.payload,
        };

      case 'remove-allOtherHandle-state': {
        return {
          ...state,
          ...otherInitState,
        };
      }
      default:
        return {
          ...state,
        };
    }
  };

  const [state, userOthDispatch] = useReducer(otherReducer, otherInitState);
  return {
    ...state,
    userOthDispatch,
  };
};
